package wifiunion.com.notice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.ArrayList;

import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.NoticeTargetRelGroup;
import wifiunion.com.notice.entity.SubNoticeTargetEntity;



/**
 * Created by Administrator on 2016/12/4.
 */

public class AdRelTargetDeviceAdapter extends BaseExpandableListAdapter {


    private Context mContext;

    private ArrayList<NoticeTargetRelGroup> flist = new ArrayList<NoticeTargetRelGroup>();
    private View.OnClickListener mitemClickListener;

    public AdRelTargetDeviceAdapter(Context mContext, ArrayList<NoticeTargetRelGroup> list , View.OnClickListener  clickListener) {
        super();

        this.mContext = mContext;
        this.mitemClickListener = clickListener;
        this.flist = list;

    }

    @Override
    public int getGroupCount() {
        if (null != flist) {
            return flist.size();
        } else {
            return 0;
        }

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (null != flist && null != flist.get(groupPosition)
                && null != flist.get(groupPosition).getChilddata()) {
            return flist.get(groupPosition).getChilddata().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (null != flist && flist.size() > 0) {
            return flist.get(groupPosition);
        } else {
            return 0;
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return flist.get(groupPosition).getChilddata().get(childPosition);

    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        ParentViewHolder parentViewHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.parent_device_group_item, null);
            parentViewHolder = new ParentViewHolder();
            parentViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.parent_selectall_cb);
            parentViewHolder.nameTv = (TextView) convertView.findViewById(R.id.pater_name_tv);
            parentViewHolder.mainView = (RelativeLayout)convertView.findViewById(R.id.device_addgroup_item_rl);
            parentViewHolder.stateIv = (ImageView) convertView.findViewById(R.id.pater_state_iv);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
        parentViewHolder.checkBox.setOnClickListener(mitemClickListener);
        parentViewHolder.checkBox.setTag(groupPosition);


        NoticeTargetRelGroup gp = (NoticeTargetRelGroup) flist.get(groupPosition);


        if (gp.getIsSelected() == 1) {
            parentViewHolder.checkBox.setSelected(true);
        } else {
            parentViewHolder.checkBox.setSelected(false);
        }

        if (isExpanded) {
            parentViewHolder.stateIv.setImageResource(R.drawable.open);

        } else {
            parentViewHolder.stateIv.setImageResource(R.drawable.close);

        }
        parentViewHolder.nameTv.setText(gp.getName());



        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {



        ChildViewHolder childViewHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.child_device_group_item, null);
            childViewHolder = new ChildViewHolder();
            childViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.child_selectall_cb);
            childViewHolder.nameTv = (TextView) convertView.findViewById(R.id.child_device_name);

            childViewHolder.mainView = (RelativeLayout) convertView.findViewById(R.id.device_addgroup_subitem_rl);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.mainView.setTag(groupPosition+"-"+childPosition);
        childViewHolder.mainView.setOnClickListener(mitemClickListener);
        childViewHolder.checkBox.setTag(groupPosition+"-"+childPosition);
        childViewHolder.checkBox.setOnClickListener(mitemClickListener);


        SubNoticeTargetEntity md = (SubNoticeTargetEntity) flist.get(groupPosition).getChilddata().get(childPosition);

        if (md.getIsSelected()==1) {
            childViewHolder.checkBox.setSelected(true);
        } else {
            childViewHolder.checkBox.setSelected(false);
        }
        childViewHolder.nameTv.setText(md.getShowName1() + "/" + md.getShowName2());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    public class ParentViewHolder {
        private RelativeLayout mainView;
        private TextView nameTv;
        private CheckBox checkBox;
        private ImageView stateIv;
    }

    public class ChildViewHolder {
        private RelativeLayout mainView;
        private TextView nameTv;
        private CheckBox checkBox;
    }
}
